﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Models.Extensions;

namespace MySQLProviderODBC.UI
{
    /// <summary>
    /// Interaction logic for EditDatabase.xaml
    /// </summary>
    public partial class EditDatabase : UserControl, SQPlugin.IEditDatabase
    {
        /// <summary>
        /// Initialise the EditDatabase user control
        /// </summary>
        public EditDatabase()
        {
            InitializeComponent();

            // Hook up data context for data binding
            DataContext = this;
        }

        /// <summary>
        /// The user name to use when connecting to the server
        /// </summary>
        public string UserName
        {
            get
            {
                return (string)GetValue(UserNameProperty);
            }
            set
            {
                SetValue(UserNameProperty, value);
            }
        }

        /// <summary>
        /// The password to use when connecting to the server
        /// </summary>
        public string Password
        {
            get
            {
                return (string)GetValue(PasswordProperty);
            }
            set
            {
                SetValue(PasswordProperty, value);
            }
        }

        /// <summary>
        /// The version of the MySQL ODB driver to use (currently 3.51 or 5.1)
        /// </summary>
        public string Version
        {
            get
            {
                return (string)GetValue(VersionProperty);
            }
            set
            {
                SetValue(VersionProperty, value);
            }
        }

        /// <summary>
        /// The port to use when connecting
        /// </summary>
        public string Port
        {
            get
            {
                return (string)GetValue(PortProperty);
            }
            set
            {
                SetValue(PortProperty, value);
            }
        }

        /// <summary>
        /// The database model being edited
        /// </summary>
        public Models.Database DatabaseToEdit
        {
            get;
            set;
        }

        #region IEditDatabase

        /// <summary>
        /// Return the updated database definition
        /// </summary>
        /// <returns>A database definition that contains changes made by the user</returns>
        public Models.Database GetUpdatedDatabase()
        {
            Dictionary<string, string> connectionDetails = DatabaseToEdit.DatabaseServer.ServerConnection;
            connectionDetails.Clear();
            connectionDetails.Add("UserName", UserName);
            connectionDetails.Add("Password", Password);
            connectionDetails.Add("Version", Version);
            connectionDetails.Add("Port", Port);
            
            return DatabaseToEdit;
        }

        /// <summary>
        /// Set the database definition that we are going to be editing
        /// </summary>
        /// <param name="database">The database definition to edit</param>
        public void SetDatabase(Models.Database database)
        {
            DatabaseToEdit = database;
            UserName = DatabaseToEdit.DatabaseServer.ServerConnection.GetString("UserName", string.Empty);
            Password = DatabaseToEdit.DatabaseServer.ServerConnection.GetString("Password", string.Empty);
            Version = DatabaseToEdit.DatabaseServer.ServerConnection.GetString("Version", string.Empty);
            Port = DatabaseToEdit.DatabaseServer.ServerConnection.GetString("Port", string.Empty);
        }

        #endregion

        /// <summary>
        /// Using a DependencyProperty as the backing store for UserName.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty UserNameProperty =
            DependencyProperty.Register("UserName", typeof(string), typeof(EditDatabase), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for Password.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.Register("Password", typeof(string), typeof(EditDatabase), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for WindowsAuthentication.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty WindowsAuthenticationProperty =
            DependencyProperty.Register("WindowsAuthentication", typeof(bool), typeof(EditDatabase), new UIPropertyMetadata(true));

        /// <summary>
        /// Using a DependencyProperty as the backing store for Port.  This enables animation, styling, binding, etc... 
        /// </summary>
        public static readonly DependencyProperty PortProperty =
            DependencyProperty.Register("Port", typeof(string), typeof(EditDatabase), new UIPropertyMetadata(null));

        /// <summary>
        /// Using a DependencyProperty as the backing store for Version.  This enables animation, styling, binding, etc...
        /// </summary>
        public static readonly DependencyProperty VersionProperty =
            DependencyProperty.Register("Version", typeof(string), typeof(EditDatabase), new UIPropertyMetadata(null));
    }
}
